import { observable, action } from 'mobx';
import { WeaForm, WeaTableNew } from 'comsMobx';
import * as Api from './../apis/postDocManageApi';
import { WeaLocaleProvider } from 'ecCom';
import { message } from 'antd';

const { TableStore } = WeaTableNew;
const { getLabel } = WeaLocaleProvider;

class PostDocManageStore {
  @observable topTab = [];// tab页签数据
  @observable searchForm = new WeaForm();// 高级搜索form表单
  @observable advancedSearchForm = new WeaForm();// 已发送搜索form
  @observable dialogForm = new WeaForm();// 新建发文 form
  @observable backoutForm = new WeaForm();// 撤销 form
  @observable invalidForm = new WeaForm();// 作废 form
  @observable tableStore = new TableStore();// 公文列表
  @observable reciveDocUnitTable = new TableStore();// 收文单位信息列表
  @observable isShow = false;// 新建发文的显示状态
  @observable selectedKey = '0';// tab页签 选项
  @observable groupid = 'Sent';// tab页签id
  @observable selectedRowKeys = [];// 行选择
  @observable showSearchAd = false;// 高级搜索下拉是否显示
  @observable showRecive = false;// 收文单位弹窗是否显示
  @observable documentIdentifier = '';// 文档标识
  @observable documentId = 0;// 文档id
  @observable unitArr = [];// 选择单位的数组
  @observable unitSelectedRowKeys = [];// 选择单位时存储的信息id
  @observable newCreat = false;// 是不是新建发文
  @observable showReasonDialog = false;// 是否显示拒收或撤销理由
  @observable dialogType = 'invalid'// 弹窗类型 撤销还是作废 invalid backout
  @observable Document_Identifier_Arr = [];// 文档标识数组
  @observable loading = false;
  @observable searchsBaseValue = '';
  @observable currentDoc = '';
  @observable rerender = 'xxx';

  // 打开新建发文表单
  @action.bound openDialog() {
    this.isShow = true;
    this.newCreat = true;
  }
  // 打开新建发文
  @action.bound newPostDoc() {
    this.dialogForm.resetForm();
  }

  @action
  setSelectedRowKeys = () => {
    this.selectedRowKeys = [];
  }

  // 关掉新建发文弹窗
  @action.bound closeDialog() {
    this.isShow = false;
    this.documentId = 0;
    this.dialogForm.resetForm();
    this.selectedRowKeys = [];
    this.currentDoc = '';
    this.tableStore.setSelectedRowKeys([]);
    this.Document_Identifier_Arr = [];
  }

  // 关闭收文单位信息弹窗
  @action.bound closeReciveDialog() {
    this.documentIdentifier = '';
    this.showRecive = false;
    this.Document_Identifier_Arr = [];
    this.unitArr = [];
    this.selectedRowKeys = [];
    this.currentDoc = '';
    this.unitSelectedRowKeys = [];
    this.documentId = 0;
    this.tableStore.setSelectedRowKeys([]);
  }
  // 获取查看收文单位信息的table数据
  @action.bound getReciveTableList(entityName = '') {
    const params = {
      documentId: this.documentIdentifier, // 公文标识
      entityName, // 搜索参数  单位名称
    };
    Api.getReciveTableList(params).then((data) => {
      if (data.sessionkey) {
        this.reciveDocUnitTable.getDatas(data.sessionkey, 1, 10);
      }
    });
  }
  // 更改Tab页签
  @action.bound changeTab(v) {
    this.selectedRowKeys = [];
    this.selectedKey = v;
    switch (v) {
      case '0': {
        this.groupid = 'Sent';
        break;
      }
      case '1': {
        this.groupid = 'Rejected';
        break;
      }
      case '2': {
        this.groupid = 'BeRefunded';
        break;
      }
      case '3': {
        this.groupid = 'Undone';
        break;
      }
      case '4': {
        this.groupid = 'Cancellation';
        break;
      }
    }
    this.advancedSearchForm.resetForm();
    this.searchForm.resetForm();
    this.getTableList();
  }
  // 获取Tab和Form数据
  @action.bound getTabFormData() {
    Api.getTabFormData().then((data) => {
      this.topTab = data.topTab;
      this.advancedSearchForm.setCondition(data.sendDateItems);
      this.searchForm.setCondition(data.dealDateItems);
    });
  }
  // 获取table数据
  @action.bound getTableList(text = '') {
    this.currentDoc = '';
    let searchFormData = {};
    if (this.groupid === 'Sent') {
      searchFormData = this.advancedSearchForm.getFormParams();
    } else {
      searchFormData = this.searchForm.getFormParams();
    }
    const params = {
      groupid: this.groupid, // tab ID
      Title: searchFormData.Title || text, // 公文标题
      shopName: searchFormData.shopName || '', // 发文字号
      operationDate: searchFormData.operationDate || '', // 发送时间或处理时间
      startDate: searchFormData.startDate || '', // 开始时间
      endDate: searchFormData.endDate || '', // 结束时间
    };
    Api.getTableList(params).then((data) => {
      if (data.sessionkey) {
        this.tableStore = new TableStore();
        this.tableStore.getDatas(data.sessionkey);
        this.showSearchAd = false;
      }
    });
  }
  // 选择行
  @action.bound onRowSelect(keyArr, rows) {
    this.Document_Identifier_Arr = [];
    this.selectedRowKeys = [];
    for (let row of rows) {
      this.Document_Identifier_Arr.push(row.document_identifier);
      this.selectedRowKeys.push(row.baseinfoid);
    }
  }
  // 查看收文情况
  @action.bound CheckReceipt() {
    this.showRecive = true;
    this.getReciveTableList();
  }
  // 作废
  @action.bound showInvalidDialog() {
    this.dialogType = 'invalid';
    this.showReasonDialog = true;
  }
  // 编辑公文
  @action.bound edit(row) {
    // this.selectedRowKeys.push(row.id);
    this.currentDoc = row.id;
    this.Document_Identifier_Arr.push(row.document_identifier);
    this.documentId = row.baseinfoid;
    this.isShow = true;
    this.newCreat = false;
  }
  // 发文管理列表的所有操作
  @action.bound onOperatesClick(record, index, operate, flag, browser) {
    this.documentId = record.id;
    // this.selectedRowKeys=[];
    // this.selectedRowKeys.push(record.id);
    this.Document_Identifier_Arr = [];
    this.Document_Identifier_Arr.push(record.document_identifier);
    switch (flag) {
      // 查看收文情况
      case '0': {
        this.documentIdentifier = record.document_identifier;
        this.currentDoc = record.id;
        this.CheckReceipt();
        break;
      }
      // 作废
      case '1': {
        this.currentDoc = record.id;
        this.showInvalidDialog();
        break;
      }
      // 重新发送
      case '2': {
        this.selectedRowKeys = [];
        this.selectedRowKeys.push(record.baseinfoid);
        browser.openModal();
        break;
      }
    }
  }
  // 收文单位信息选择行
  @action.bound onUnitRowSelect(selectedRowKeys, selectedRows) {
    this.unitArr = [];
    this.unitSelectedRowKeys = [];
    for (let row of selectedRows) {
      this.unitArr.push(row.receive_companyid);
      this.unitSelectedRowKeys.push(row.id);
    }
  }
  // 获取作废、撤销的form
  @action.bound getInvalidForm() {
    Api.getInvalidForm().then((data) => {
      this.invalidForm.setCondition(data.conditions1);
      this.backoutForm.setCondition(data.conditions2);
    });
  }
  // 理由弹窗的关闭
  @action.bound hideReasonDialog() {
    this.showReasonDialog = false;
    this.invalidForm.updateFields({ detail: { value: '' } });
    this.backoutForm.updateFields({ detail: { value: '' } });
    this.selectedRowKeys = [];
    this.tableStore.setSelectedRowKeys([]);
    this.rerender = Math.random();
    this.currentDoc = '';
  }
  // 撤销
  @action.bound backout(record = {}) {
    this.dialogType = 'backout';
    this.showReasonDialog = true;
    if (record.receive_companyid) {
      this.unitArr = [];
      this.unitArr.push(record.receive_companyid);
      this.unitSelectedRowKeys = [];
      this.unitSelectedRowKeys.push(record.id);
    }
  }
  // 撤销提交
  @action.bound backoutSubmit() {
    const formData = this.backoutForm.getFormParams();
    const ids = this.unitSelectedRowKeys.join(',');
    const Document_Identifier = this.Document_Identifier_Arr.join(',');
    const receiveUnitIds = this.unitArr.join(',');
    const params = {
      ids, // 每条收文数据的id
      detail: formData.detail, // 处理意见
      status: '4', // 撤销的状态
      Document_Identifier, // 公文标识
      receiverDepartment: receiveUnitIds, // 单位id
    };
    Api.backoutInvalidSend(params).then((data) => {
      if (data.api_status) {
        this.backoutForm.updateFields({ detail: { value: '' } });
        this.rerender = Math.random();
        this.unitArr = [];
        this.showReasonDialog = false;
        this.showRecive = false;
        this.getReciveTableList();
        this.getTableList();
        this.unitSelectedRowKeys = [];
        this.selectedRowKeys = [];
        this.tableStore.setSelectedRowKeys([]);
        message.success(data.api_msg);
      } else {
        message.error(data.api_errormsg);
      }
    });
  }
  // 作废
  @action.bound invalidSubmit() {
    const formData = this.invalidForm.getFormParams();
    const ids = this.currentDoc ? this.currentDoc : this.selectedRowKeys.join(',');
    const Document_Identifier = this.Document_Identifier_Arr.join(',');
    const params = {
      ids,
      detail: formData.detail,
      status: '5',
      Document_Identifier,
    };
    Api.backoutInvalidSend(params).then((data) => {
      if (data.api_status) {
        message.success(data.api_msg);
        this.getTableList();
        this.showReasonDialog = false;
        this.selectedRowKeys = [];
        this.tableStore.setSelectedRowKeys([]);
        this.documentId = 0;
        this.invalidForm.updateFields({ detail: { value: '' } });
        this.rerender = Math.random();
      } else {
        message.error(getLabel('30651', '操作失败'));
      }
    });
  }
  // 重新发送
  @action.bound resendSubmit(unitIds) {
    this.loading = true;
    const ids = this.currentDoc ? this.currentDoc : this.selectedRowKeys.join(',');
    const Document_Identifier = this.Document_Identifier_Arr.join(',');
    const receiveUnitIds = unitIds || this.unitArr.join(',');
    const params = {
      ids,
      status: '0',
      Document_Identifier,
      receiverDepartment: receiveUnitIds,
    };
    Api.backoutInvalidSend(params).then((data) => {
      this.loading = false;
      if (data.api_status) {
        message.success(data.api_msg);
        this.closeReciveDialog();
        this.closeDialog();
        this.getReciveTableList();
        this.getTableList();
      } else {
        message.error(data.api_errormsg);
      }
    });
  }
}
export default new PostDocManageStore();
